﻿using ReflectionStudio.Core.Events;
using System;
using System.Data;
using System.Linq;
using WPFExcelReport.Helpers;
namespace WPFExcelReport
{
    public partial class ITXEXRPT
    {
        private void YGRAB50GB12(DataSet db)
        {
            try
            {
                string value = "";
                DataRow row = null;
                int x = 1, y = 1;
                x = 3; y = 5;
                string str1 = "", str2 = "", str3 = "";
                DataRow dwOrder = db.Tables["ABPOS"].Rows[0];
                string KRS = dwOrder["ABPAUFKRS"].ToString().Trim();
                string LFD = dwOrder["ABPAUFLFD"].ToString().Trim();
                string POS = dwOrder["ABPAUFPOS"].ToString().Trim();
                double TarGetRatio = Convert.ToDouble(dwOrder["ABPSTKLG"].ToString()) / 10000000;
                if (TarGetRatio == 0)
                    TarGetRatio = 1;
                value = KRS + "-" + Helper.GetSubRollNum(LFD) + "-" + Helper.GetSubRollNum(POS);
                exAgent.SetCellValue(3, 5, value);
                exAgent.SetCellValue(1, 2, "混棉联系单(" + value + ")");
                x = 3; y = 30;
                Helper.GetDate(ref date, ref year, ref month, ref day);
                Helper.GetSplitDate(ref str1, ref str2, ref str3, date);
                exAgent.SetCellValue(2, 37, str3);
                int positions = 0;
                if (!db.Tables.Contains("ABAUFM"))
                {
                    return;
                }
                string ABPART, ABPVAR, ABPFARBE;

                ABPART = dwOrder["ABPART"].ToString();
                ABPVAR = dwOrder["ABPVAR"].ToString();
                ABPFARBE = dwOrder["ABPFARBE"].ToString();

                exAgent.SetCellValue(3, 14, ABPART);
                exAgent.SetCellValue(4, 14, ABPFARBE);
                var query = from DataRow dwRow in db.Tables["GSCOLOR"].AsEnumerable()
                            where dwRow["GSCART"].ToString() == ABPART
                            && dwRow["GSCFARBE"].ToString() == ABPFARBE
                            select dwRow;
                foreach (DataRow dwRow in query)
                {
                    exAgent.SetCellValue(4, 19, GetGB2312Value(dwRow["GSCBEZ"].ToString()));
                }
                string gpm = "";
                string MU = "";
                double batchqty = 0;
                double qty = 0;
                query = from DataRow dwRow in db.Tables["TBABA"].AsEnumerable()
                        where dwRow["ABAVKAUFLFD"].ToString() == "000000"
                        && dwRow["ABAAUFKRS"].ToString() == KRS
                        && dwRow["ABAAUFLFD"].ToString() == LFD
                        && dwRow["ABAAUFPOS"].ToString() == POS
                        select dwRow;

                int idx = 0;
                int idxmu = 0;
                foreach (DataRow dwRow in query)
                {
                    MU = dwRow["ABAAUFMSL"].ToString();
                    double ABARESMG = Convert.ToDouble(dwRow["ABARESMG"].ToString()) * 0.001;
                    double ABARSTSGEW = Convert.ToDouble(dwRow["ABARSTSGEW"].ToString()) * 0.001;

                    if (ABARESMG == 0 && ABARSTSGEW == 0) continue;
                    string mu = Helper.GetSDDAT(db, "0015", MU, "V");
                    exAgent.SetCellValue(5 + idx, 11, "定量");
                    exAgent.SetCellValue(5 + idx, 14, mu.Replace("粗纱定量", ""));
                    exAgent.SetCellValue(5 + idx, 17, "重量");
                    if (ABARESMG > 0)
                    {
                        exAgent.SetCellValue(5 + idx, 19, ABARESMG);
                    }
                    else
                    {
                        exAgent.SetCellValue(5 + idx, 19, ABARSTSGEW);
                    }
                    idx++;
                }
                DataRow dwData = INTEXDB.ICC_QUERYTB.NewRow();
                dwData["ABAMANDANT"] = app.Mandant;
                dwData["ABAAUFKRS"] = KRS;
                dwData["ABAAUFLFD"] = LFD;
                dwData["ABAAUFPOS"] = POS;
                DataSet SResult = INTEXDBC.QueryABAUFM(dwData, 11);
                query = from DataRow dwRow in SResult.Tables[0].AsEnumerable()
                        where dwRow["ABAVKAUFTYP"].ToString() == "K"
                        select dwRow;
                idx = 0;
                idxmu = 0;
                foreach (DataRow dwRow in query)
                {
                    double ABARESMG = Convert.ToDouble(dwRow["ABARESMG"].ToString()) * 0.001;
                    var qVK = from DataRow VKP in db.Tables["VKPOS"].AsEnumerable()
                              where VKP["VKPAUFKRS"].ToString() + "-" + VKP["VKPAUFLFD"].ToString() + "-" + VKP["VKPAUFPOS"].ToString() == dwRow["ORDERNO"].ToString()
                              select VKP;
                    foreach (DataRow VKP in qVK)
                    {
                        string ORDERNO = VKP["VKPAUFKRS"].ToString().Trim() + "-" + Helper.GetSubRollNum(VKP["VKPAUFLFD"].ToString()) + "-" + Helper.GetSubRollNum(VKP["VKPAUFPOS"].ToString());
                        string VKPKDARTNR = VKP["VKPKDARTNR"].ToString();
                        if (VKPKDARTNR.Trim() == "" || VKPKDARTNR.Trim() == ".")
                        {
                            ORDERNO = GetGB2312Value(VKP["KDKUNDNA"].ToString().Trim()) + "(" + ORDERNO + ")";
                        }
                        else
                        {
                            ORDERNO = VKPKDARTNR.Trim() + "(" + ORDERNO + ")";
                        }
                        exAgent.SetCellValue(4 + idxmu, 22, ORDERNO);

                        exAgent.SetCellValue(4 + idxmu, 27, dwRow["ABAKDPARTIE"].ToString());
                        exAgent.SetCellValue(4 + idxmu, 31, Convert.ToDouble(dwRow["ABARESMG"].ToString()) / 1000);
                        exAgent.SetCellValue(4 + idxmu, 34, VKP["VKPLIEFTERM"].ToString());
                        if (dwRow["ABARESPROG"].ToString()=="BD")
                        {
                            exAgent.SetCellValue(4 + idxmu, 34, VKP["ABARESDAT"].ToString()); 
                        }
                        
                        exAgent.SetCellValue(4 + idxmu, 37, VKP["YARNTYP"].ToString().Trim() + dwRow["YARNNE"].ToString());
                        exAgent.SetCellValue(3, 14, VKP["YARNTYP"].ToString().Trim() + dwRow["YARNNE"].ToString());
                    }
                    idxmu++;
                }
                query = from DataRow dwRow in db.Tables["GAAUF"].AsEnumerable()
                        where dwRow["GAAUFKRS"].ToString() == KRS
                            && dwRow["GAAUFLFD"].ToString() == LFD
                            && dwRow["GAAUFPOS"].ToString() == POS
                            && dwRow["GASA"].ToString() == "713"
                        select dwRow;
                int count = query.Count();

                double kg = 0, kg3 = 0;
                x = 0;
                string ndate = "";
                int nRow = 2;
                int xStart = 24;
                idx = 0;

                foreach (DataRow dwRow in query)
                {
                    ABPART = dwRow["GAART"].ToString();
                    ABPVAR = dwRow["GAVAR"].ToString();
                    ABPFARBE = dwRow["GAFARBE"].ToString();
                    string GSHANDLNR = dwRow["GSHANDLNR"].ToString();
                    string GSGARNBEZ = dwRow["GSGARNBEZ"].ToString();

                    kg = System.Convert.ToDouble(dwRow["GAKG3"].ToString()) * 0.01 + System.Convert.ToDouble(dwRow["GAKG5"].ToString()) * 0.01;
                    kg = System.Convert.ToDouble(dwRow["GAKG1"].ToString()) * 0.01;
                    string krs, lfd, pos, apos;
                    //krs = dwRow["ABAAUFKRS"].ToString();
                    //lfd = dwRow["ABAAUFLFD"].ToString();
                    //pos = dwRow["ABAAUFPOS"].ToString();
                    apos = dwRow["GAPOS"].ToString();
                    #region=========
                    double MQTY = 0;
                    string BATCHPATTERN = "";
                    var qMU = from MUrow in db.Tables["GAAUF"].AsEnumerable()
                              where MUrow["GAAUFKRS"].ToString() == KRS
                              && MUrow["GAAUFLFD"].ToString() == LFD
                              && MUrow["GAAUFPOS"].ToString() == POS
                              && MUrow["GASA"].ToString() == "713"
                              group MUrow by new
                              {
                                  APOS = MUrow["GAPOS"].ToString(),
                              }
                                  into g
                                  select new
                                  {
                                      KEY = g.Key,
                                      MQTY = g.Sum(XD => Convert.ToDouble(XD["GAKG3"].ToString()) / 100 + Convert.ToDouble(XD["GAKG5"].ToString()) / 100),
                                      RMQTY = g.Sum(XD => Convert.ToDouble(XD["GAKG1"].ToString()) / 100),
                                  };
                    foreach (var mu in qMU)
                    {
                        if (mu.KEY.APOS == apos)
                        {
                            if (mu.MQTY > 0)
                            {
                                MQTY = mu.MQTY;
                            }
                            else
                            {
                                MQTY = mu.RMQTY;
                            }
                        }
                        if (idx == 0)
                        {
                            BATCHPATTERN += Convert.ToInt16(apos) - 1 + ":" + MQTY + ";";
                            if (mu.MQTY > 0)
                            {
                                qty += mu.MQTY;
                            }
                            else
                            {
                                qty += mu.RMQTY;
                            }
                        }
                    }
                    if (idx == 0)
                    {
                        //BATCHPATTERN += "***";
                        //BATCHPATTERN = BATCHPATTERN.Replace(";***", "");
                        //exAgent.SetCellValue(22, 29, BATCHPATTERN);
                        exAgent.SetCellValue(22, 34, qty);
                    }
                    if (MQTY == 0)
                        MQTY = 1;
                    if (qty == 0)
                        qty = 1;
                    #endregion
                    MU = dwRow["GAAUFMSL"].ToString();
                    double RATIO = Convert.ToDouble(dwRow["GABASMG2"].ToString()) / 1000;
                    MU = Helper.GetSDDAT(db, "0015", MU, "V");
                    //string code = "*" + krs + lfd + pos + apos + "*";
                    string code = "*" + apos + "*";
                    exAgent.SetCellValue(xStart + idx * nRow, 1, Convert.ToInt16(1));
                    //exAgent.SetCellValue(xStart + idx * nRow, 3, code);
                    exAgent.SetCellValue(xStart + idx * nRow, 17, MU);
                    //GSHANDLNR,GSGARNBEZ
                    exAgent.SetCellValue(xStart + idx * nRow, 19, GetGB2312Value(GSGARNBEZ).Trim());// + ABPVAR.Trim());
                    exAgent.SetCellValue(xStart + idx * nRow + 1, 19, ABPFARBE);
                    //exAgent.SetCellValue(xStart + idx * nRow, 31, 0.001 * Convert.ToDouble(dwRow["ABAVKART"].ToString()));//盘数
                    //exAgent.SetCellValue(xStart + idx * nRow, 33, dwRow["ABAVKFARBE"].ToString().Trim());//盘数
                    exAgent.SetCellValue(xStart + idx * nRow, 24, dwRow["GABEM"].ToString().Trim());
                    exAgent.SetCellValue(xStart + idx * nRow, 34, kg);
                    exAgent.SetCellValue(xStart + idx * nRow, 29, Math.Round(RATIO / TarGetRatio, 2) + "%");
                    #region=====Alloction======= 
                    var qzu = from rows in db.Tables["ZUTEI"].AsEnumerable()
                              where rows["ZUAUFKRS"].ToString() == KRS
                                 & rows["ZUAUFLFD"].ToString() == LFD
                                & rows["ZUAUFPOS"].ToString() == POS
                                & rows["ZUPOS"].ToString() == apos
                              select rows;

                    foreach (DataRow rw in qzu)
                    {
                        string sppartie = rw["ZUSPPART"].ToString().Trim();
                        //exAgent.SetCellValue(xStart + idx * nRow, 24, Helper.GetGB2312Value(sppartie));
                        exAgent.SetCellValue(xStart + idx * nRow, 27, rw["GLLB"].ToString() + Environment.NewLine + rw["GLLP"].ToString());
                        break;
                    } 
                    #endregion
                    idx++;
                    if (idx < count)
                    {
                        exAgent.RangeCopy(24, 25, xStart + idx * nRow, xStart + idx * nRow + 1);
                    }
                }
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }
        private void YGRAB50GB12x()
        {
            try
            {
                string value = "";
                DataRow row = null;
                int x = 1, y = 1;
                x = 3; y = 5;
                string str1 = "", str2 = "", str3 = "";
                row = dsSource.Tables["ABKOPF"].Rows[0];
                value = row["ABKAUFKRS"].ToString() + "-" + row["ABKAUFLFD"].ToString();
                exAgent.SetCellValue(x, y, value);
                value = row["ABKKDAUFTNR"].ToString();
                x = 3; y = 14;
                exAgent.SetCellValue(x, y, value);
                x = 3; y = 30;
                Helper.GetDate(ref date, ref year, ref month, ref day);
                Helper.GetSplitDate(ref str1, ref str2, ref str3, date);
                exAgent.SetCellValue(3, 30, str3);
                str1 = row["ABKNEUDAT"].ToString();
                Helper.GetSplitDate(ref str1, ref str2, ref str3, str1);
                exAgent.SetCellValue(4, 30, str3);
                /*
                               if (dsSource.Tables.Contains("LIEFDAT"))
                               {
                                   if (dsSource.Tables["LIEFDAT"].Rows.Count > 0)
                                   {
                                       row = dsSource.Tables["LIEFDAT"].Rows[0];
                                       value = row["LSNAME1"].ToString().Trim() + row["LSNAME2"].ToString();
                                       value = GetGB2312Value(value);
                                       x = 4; y = 5;
                                       exAgent.SetCellValue(x, y, value);
 
                                       value = row["LSSTRASSE1"].ToString();
                                       value = GetGB2312Value(value);
                                       x = 5; y = 5;
                                       exAgent.SetCellValue(x, y, value); 
                                   }
                               }*/
                int rownum = -1;
                string remark = "";
                value = "";

                int positions = 0;
                if (!dsSource.Tables.Contains("GAAUF"))
                {
                    return;
                }
                var query = from DataRow ga in dsSource.Tables["GAAUF"].AsEnumerable()
                            where ga["GASA"].ToString() == "713"
                            select ga;
                int count = query.Count();
                string art = "", var = "", color = "";
                double kg = 0, kg3 = 0;
                x = 0;
                string ndate = "";
                int rpl = 2;
                int posx = 12;
                foreach (DataRow ga in query)
                {
                    positions++;
                    art = ga["GAART"].ToString();
                    var = ga["GAVAR"].ToString();
                    color = ga["GAFARBE"].ToString();

                    kg = System.Convert.ToDouble(ga["GAKG1"].ToString()) * 0.01;
                    kg3 = System.Convert.ToDouble(ga["GAKG3"].ToString()) * 0.01;
                    if (kg3 == 0)
                    {
                        count--;
                        continue;
                    }
                    ndate = ga["GADATVON"].ToString();
                    Helper.GetSplitDate(ref str1, ref str2, ref str3, ndate);
                    exAgent.SetCellValue(posx, 20, str3);
                    exAgent.SetCellValue(posx, 1, Helper.GetSubRollNum(ga["GAAUFPOS"].ToString()), true);
                    exAgent.SetCellValue(posx, 24, kg, false);
                    #region=====Technical============
                    var query1 = from rows in dsSource.Tables["GSDAT"].AsEnumerable()
                                 where rows["GSART"].ToString() == art
                                 & rows["GSVAR"].ToString() == var
                                 select rows;
                    foreach (DataRow rw in query1)
                    {
                        value = GetGB2312Value(rw["GSGARNBEZ"].ToString());
                        exAgent.SetCellValue(posx, 3, art + value.Trim());
                    }
                    #endregion
                    #region=====COLOR============
                    if (dsSource.Tables.Contains("GSCOLOR"))
                    {
                        var queryc = from rows in dsSource.Tables["GSCOLOR"].AsEnumerable()
                                     where rows["GSCART"].ToString() == art
                                     & rows["GSCVAR"].ToString() == var
                                     & rows["GSCFARBE"].ToString() == color
                                     select rows;
                        foreach (DataRow rw in queryc)
                        {
                            value = GetGB2312Value(rw["GSCBEZ"].ToString());
                            exAgent.SetCellValue(posx, 16, color.Trim() + value.Trim());
                            if (color.Trim() == "000000")
                            {
                                exAgent.SetCellValue(posx, 16, value.Trim());
                            }
                        }
                    }
                    #endregion
                    #region=====composition=======
                    if (dsSource.Tables.Contains("GSMATK"))
                    {
                        var query2 = from rows in dsSource.Tables["GSMATK"].AsEnumerable()
                                     where rows["GSMART"].ToString() == art
                                     & rows["GSMVAR"].ToString() == var
                                     select rows;
                        string composition = "";
                        foreach (DataRow rw in query2)
                        {
                            string sg = rw["GSMRBEZ"].ToString().Trim();
                            string bez = rw["GSMRSPZ"].ToString();
                            bez = GetGB2312Value(bez).Trim();
                            double ratio = System.Convert.ToDouble(rw["GSMRANT"].ToString()) * 0.1;
                            composition += ratio + "%" + sg;
                        }
                        exAgent.SetCellValue(posx + 1, 5, composition);
                    }
                    #endregion
                    #region=====Alloction=======
                    if (dsSource.Tables.Contains("ZUTEI"))
                    {
                        var qzu = from rows in dsSource.Tables["ZUTEI"].AsEnumerable()
                                  where rows["ZUAUFKRS"].ToString() == ga["GAAUFKRS"].ToString()
                                     & rows["ZUAUFLFD"].ToString() == ga["GAAUFLFD"].ToString()
                                    & rows["ZUAUFPOS"].ToString() == ga["GAAUFPOS"].ToString()
                                     & rows["ZUSA"].ToString() == ga["GASA"].ToString()
                                     & rows["ZUPOS"].ToString() == ga["GAPOS"].ToString()
                                  select rows;
                        string filters = "";
                        DataRow[] rx;
                        DataRow dwPartie = null;
                        string info = "";
                        int zucount = qzu.Count();
                        int zupos = 0;
                        foreach (DataRow rw in qzu)
                        {
                            // ZUPARTIEZULO	ZULB ZUEINTKG ZUENTNKG
                            double allocated = System.Convert.ToDouble(rw["ZUEINTKG"].ToString().Trim()) * 0.001;
                            double get = System.Convert.ToDouble(rw["ZUENTNKG"].ToString().Trim()) * 0.001;
                            double TOGET = allocated - get;
                            string sppartie = rw["SELLER"].ToString().Trim() + "-" + rw["ZUSPPART"].ToString().Trim();
                            if (TOGET == 0)
                            {
                                zucount--;
                                continue;
                            }
                            exAgent.SetCellValue(posx + zupos, 27, Helper.GetGB2312Value(sppartie));
                            exAgent.SetCellValue(posx + zupos, 31, rw["GLLP"].ToString().Trim());
                            exAgent.SetCellValue(posx + zupos, 33, TOGET);
                            zupos++;
                            if (zupos > 1
                             && zupos < zucount
                             && zucount > 2)
                            {
                                exAgent.RangeCopy(13, 13, posx + zupos, posx + zupos);
                            }
                        }
                        if (zupos > 2)
                        {
                            posx = posx + zupos - 2;
                        }
                    }
                    posx = posx + rpl;
                    #endregion
                    if (positions < count)
                    {
                        exAgent.RangeCopy(12, 12 + rpl - 1, posx, posx + rpl - 1);
                    }
                }
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }

    }
}